<template>
  <el-dialog
    top="30px"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <el-tabs v-model="activeName" v-loading="loading">
      <el-tab-pane label="委托担保合同" name="first">
        <contract-entrust
          :related-data="related"
          @close="beforeClose"
          @nextTag="toNextTag"
          @letter="getUnion"
        ></contract-entrust>
      </el-tab-pane>
      <el-tab-pane label="反担保合同" name="secoend">
        <contract-guard :query-id="queryId" :related-data="related"></contract-guard>
      </el-tab-pane>
      <el-tab-pane label="合同审批">
        <contract-approve
          :query-id="queryId"
          @dismiss="beforeClose"
          @success="handleSuccess"
        ></contract-approve>
      </el-tab-pane>
      <el-tab-pane label="共同担保责任函" v-if="!isUnion">
        <union-letter :query-id="queryId" ref="union"></union-letter>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import ContractApprove from '@/credit/views/county/unionProvince/components/contractApproveDeploy/Main'
import ContractEntrust from '@/credit/views/county/unionProvince/components/ContractEntrust'
import ContractGuard from '@/credit/views/county/unionProvince/components/ContractGuard'
import unionLetter from '@/credit/views/county/unionProvince/components/unionLetter/Index'
import { mapGetters } from 'vuex'
export default {
  props: {
    visible: Boolean,
    relatedData: {
      type: Object,
      default: () => ({}),
    },
  },
  components: {
    ContractApprove,
    ContractEntrust,
    ContractGuard,
    unionLetter,
  },
  data() {
    return {
      activeName: 'first',
      loading: false,
      related: {},
    }
  },
  computed: {
    ...mapGetters(['user']),
    isUnion() {
      return this.user?.scopeArr?.includes('dirGuaPro') ?? false
    },
    queryId() {
      return this.relatedData.customer_related_id
    },
  },
  watch: {
    visible() {
      this.$store.dispatch('getEntrustContract', this.queryId)
    },
    queryId: {
      handler: function () {
        this.getData()
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    getData() {
      this.related = {}
      if (!this.queryId) {
        return
      }
      this.loading = true
      this.$api
        .getData(this.$serviceName, 'customer_related', {
          'customer_related_id.eq': this.queryId,
        })
        .then(res => {
          this.related = res?.data?.[0] ?? {}
        })
        .finally(() => {
          this.loading = false
        })
    },
    toNextTag() {
      this.activeName = 'secoend'
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.$emit('success', true)
      this.beforeClose()
    },
    getUnion() {
      this.$refs.union?.refresh()
    },
  },
}
</script>
<style lang="scss" scoped></style>
